﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using ApplLogic.ApplClasses;
using TCPClient.UI.LogicForUserControls;

namespace TCPClient.UI.UserControls
{
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class WeekTableUserControl : UserControl
    {
        private DateTime m_StartOfWeek;
        private DateTime m_EndOfWeek;
        private List<DayInAWeekUserControl> m_Days = new List<DayInAWeekUserControl>(7); 

        public WeekTableUserControl()
        {
            InitializeComponent();
        }

        public List<DayInAWeekUserControl> Days
        {
            get { return m_Days; }
        }

        private void setDaysToList()
        {
            dayInAWeekUserControlSunday.DayOfWeek = DayOfWeek.Sunday;
            m_Days.Add(dayInAWeekUserControlSunday);
            dayInAWeekUserControlMonday.DayOfWeek = DayOfWeek.Monday;
            m_Days.Add(dayInAWeekUserControlMonday);
            dayInAWeekUserControlTuesday.DayOfWeek = DayOfWeek.Tuesday;
            m_Days.Add(dayInAWeekUserControlTuesday);
            dayInAWeekUserControlWednesday.DayOfWeek = DayOfWeek.Wednesday;
            m_Days.Add(dayInAWeekUserControlWednesday);
            dayInAWeekUserControlThursday.DayOfWeek = DayOfWeek.Thursday;
            m_Days.Add(dayInAWeekUserControlThursday);
            dayInAWeekUserControlFriday.DayOfWeek = DayOfWeek.Friday;
            m_Days.Add(dayInAWeekUserControlFriday);
            dayInAWeekUserControlSaturday.DayOfWeek = DayOfWeek.Saturday;
            m_Days.Add(dayInAWeekUserControlSaturday);
        }

        public void SetWeekData(DateTime startOfWeek, DateTime endOfWeek)
        {
            if (m_Days.Count == 0)
            {
                setDaysToList();
                setLocationsOfAllShiftsInDays();
                setLabelsOfShiftLenght();
            }
            m_StartOfWeek = startOfWeek;
            m_EndOfWeek = endOfWeek;
            initializeHoursOfAllWeekDays();       
        }

        private void setLabelsOfShiftLenght()
        {
            for(int i = 0; i < DefaultConfigurationHandler.Instance.NumberOfShiftsPerDay; i++)
            {
                shiftTimesContainerUserControl1.AddSingleShiftTimeCell(m_StartOfWeek, i);
            }
        }

        private void initializeHoursOfAllWeekDays()
        {
            int numOfHoursToAdd = 0;
            int labelIndex = 0;
            foreach (var dayInAweek in m_Days)
            {
                dayInAweek.Date = m_StartOfWeek.AddHours(numOfHoursToAdd);
                numOfHoursToAdd += 24;
                dayInAweek.SetTimesOfShiftCells();
                this.daysLabelsUserControl1.SetLabelOfText(labelIndex, dayInAweek.DayOfWeek.ToString() +
                "\n" + dayInAweek.Date.ToShortDateString());
                labelIndex++;
            }   
        }

        private void setLocationsOfAllShiftsInDays()
        {
            foreach (var dayUserControl in m_Days)
            {
                dayUserControl.AddControlsOfShifts();
            }
        }

        public DateTime StartOfWeek
        {
            get { return m_StartOfWeek; }
            set { m_StartOfWeek = value; }
        }

        public DateTime EndOfWeek
        {
            get { return m_EndOfWeek; }
            set { m_EndOfWeek = value; }
        }

        public void DrawShiftsThatWorkersRegisterdTo(DateTime startTime, DateTime endTime)
        {
            foreach (var dayUserControl in m_Days)
            {
                dayUserControl.DrawShiftsThatWorkersRegisterdTo(startTime, endTime);
            }
        }

        public void DrawShiftsThatWorkersRequested(DateTime startTime, DateTime endTime, eType type)
        {
            foreach (var dayUserControl in m_Days)
            {
                dayUserControl.DrawShiftsThatWorkersRequested(startTime, endTime, type);
            }
        }

        public void CleanWeekBoard()
        {
            foreach (var day in m_Days)
            {
                day.cleanDay();
            }
        }

        public void SetTheGreenRedControlToEachShiftCell(RedGreenSelectionUserControl redGreenSelectionUserControl1)
        {
            foreach (var dayUserControl in m_Days)
            {
                dayUserControl.SetTheGreenRedControlToEachShiftCell(redGreenSelectionUserControl1);
            }
        }

    }
}
